package com.zkingsoft.actions.admin;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.model.Matchs;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.Referee;
import com.zkingsoft.model.Sort;
import com.zkingsoft.model.Team;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.MatchsService;
import com.zkingsoft.services.RefereeService;
import com.zkingsoft.services.SortService;
import com.zkingsoft.services.TeamService;

/**
 * @project MatrixCms
 * @package com.zkingsoft.actions.admin
 * @class MatchsController.java
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2016年4月21日10:31
 * @description 球队管理
 */
@Controller
@RequestMapping(value = "/admin/matchs")
public class MatchsController extends BaseController {

	private static final Logger log = Logger.getLogger(MatchsController.class);

	@Resource
	private MatchsService currentService;
	@Resource
	private SortService sortService;
	@Resource
	private RefereeService refereeService;
	@Resource
	private TeamService teamService;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, Matchs matchs, PaginationVO pageVo) {
		log.info("按钮列表...");
		//referee.setCompanyId(getMe(request).getCompanyId());
		matchs.setRank(10);
		return showList(currentService, matchs, pageVo);
	}
	
	@RequestMapping(value = "/groupScore")
	public @ResponseBody AjaxResult getGroupScore(HttpServletRequest request, Team team, PaginationVO pageVo){
		return showList(teamService, team, pageVo);
	}
	//8强列表
	@RequestMapping(value = "/matchs8List")
	public @ResponseBody AjaxResult getMatchs8List(HttpServletRequest request, Matchs matchs, PaginationVO pageVo){
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		matchs.setRank(8);
		pageVo.setTotalRecold(currentService.findTotal(matchs));
		List<Matchs> dataList = currentService.findInPage(matchs, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList, pageVo.getTotalRecold(),
				pageVo.getPageCount());
		return result;
	}
	//4强列表
	@RequestMapping(value = "/matchs4List")
	public @ResponseBody AjaxResult getMatchs4List(HttpServletRequest request, Matchs matchs, PaginationVO pageVo){
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		matchs.setRank(4);
		pageVo.setTotalRecold(currentService.findTotal(matchs));
		List<Matchs> dataList = currentService.findInPage(matchs, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList, pageVo.getTotalRecold(),
				pageVo.getPageCount());
		return result;
	}
	//半决赛列表
		@RequestMapping(value = "/matchs2List")
		public @ResponseBody AjaxResult getMatchs2List(HttpServletRequest request, Matchs matchs, PaginationVO pageVo){
			if (pageVo == null) {
				pageVo = new PaginationVO();
			}
			matchs.setRank(2);
			pageVo.setTotalRecold(currentService.findTotal(matchs));
			List<Matchs> dataList = currentService.findInPage(matchs, pageVo);
			AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList, pageVo.getTotalRecold(),
					pageVo.getPageCount());
			return result;
		}
		//决赛列表
		@RequestMapping(value = "/matchs1List")
		public @ResponseBody AjaxResult getMatchs1List(HttpServletRequest request, Matchs matchs, PaginationVO pageVo){
			if (pageVo == null) {
				pageVo = new PaginationVO();
			}
			matchs.setRank(1);
			pageVo.setTotalRecold(currentService.findTotal(matchs));
			List<Matchs> dataList = currentService.findInPage(matchs, pageVo);
			AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList, pageVo.getTotalRecold(),
					pageVo.getPageCount());
			return result;
		}

	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Matchs matchs) {
		if (matchs.getId() != null) {
			return modify(currentService, matchs, "小组赛");
		} else {
			matchs.setCreateTime(new Date());
			matchs.setRank(10);
			return add(currentService, matchs, "小组赛");
			
		}
	}
	//添加8强
	@RequestMapping(value = "/addOrModifyMatchs8")
	public @ResponseBody AjaxResult addOrModifyMatchs8(Matchs matchs) {
		if (matchs.getId() != null) {
			return modify(currentService, matchs, "8强");
		} else {
			matchs.setCreateTime(new Date());
			matchs.setRank(8);
			return add(currentService, matchs, "8强");
		}
	}
	//添加4强
	@RequestMapping(value = "/addOrModifyMatchs4")
	public @ResponseBody AjaxResult addOrModifyMatchs4(Matchs matchs) {
		if (matchs.getId() != null) {
			return modify(currentService, matchs, "4强");
		} else {
			matchs.setCreateTime(new Date());
			matchs.setRank(4);
			return add(currentService, matchs, "4强");
		}
	}
	//添加半决赛
		@RequestMapping(value = "/addOrModifyMatchs2")
		public @ResponseBody AjaxResult addOrModifyMatchs2(Matchs matchs) {
			if (matchs.getId() != null) {
				return modify(currentService, matchs, "半决赛");
			} else {
				matchs.setCreateTime(new Date());
				matchs.setRank(2);
				return add(currentService, matchs, "半决赛");
			}
		}
		//添加决赛
		@RequestMapping(value = "/addOrModifyMatchs1")
		public @ResponseBody AjaxResult addOrModifyMatchs1(Matchs matchs) {
			if (matchs.getId() != null) {
				return modify(currentService, matchs, "决赛");
			} else {
				matchs.setCreateTime(new Date());
				matchs.setRank(1);
				return add(currentService, matchs, "决赛");
			}
		}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		Matchs matchs;
		if (id != null) {
			matchs = currentService.findById(id);
			request.setAttribute("obj", matchs);
		}
		try {
			List<Sort> sort = sortService.findByModel(null);
			List<Referee> referee = refereeService.findByModel(null);
			List<Team> team = teamService.findByModel(null);
			if(sort!=null&&sort.size()>0){
				request.setAttribute("sort", sort);
			}
			if(referee!=null&&referee.size()>0){
				request.setAttribute("referee", referee);
			}
			if(team!=null&&team.size()>0){
				request.setAttribute("team", team);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "admin/group-form";
	}
	//编辑8强
	@RequestMapping(value = "/editMatchs8")
	public String editMatchs8Form(Long id, HttpServletRequest request){
		Matchs matchs;
		if (id != null) {
			matchs = currentService.findById(id);
			request.setAttribute("obj", matchs);
		}
		try {
			List<Referee> referee = refereeService.findByModel(null);
			List<Team> team = teamService.findByModel(null);
			List<Sort> sort = sortService.findByModel(null);
			if(sort!=null&&sort.size()>0){
				request.setAttribute("sort", sort);
			}
			if(referee!=null&&referee.size()>0){
				request.setAttribute("referee", referee);
			}
			if(team!=null&&team.size()>0){
				request.setAttribute("team", team);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "admin/matchs8-form";
	}
	//编辑4强
	@RequestMapping(value = "/editMatchs4")
	public String editMatchs4Form(Long id, HttpServletRequest request){
		Matchs matchs;
		if (id != null) {
			matchs = currentService.findById(id);
			request.setAttribute("obj", matchs);
		}
		try {
			List<Referee> referee = refereeService.findByModel(null);
			List<Team> team = teamService.findByModel(null);
			List<Sort> sort = sortService.findByModel(null);
			if(sort!=null&&sort.size()>0){
				request.setAttribute("sort", sort);
			}
			if(referee!=null&&referee.size()>0){
				request.setAttribute("referee", referee);
			}
			if(team!=null&&team.size()>0){
				request.setAttribute("team", team);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "admin/matchs4-form";
	}
	//编辑半决赛
		@RequestMapping(value = "/editMatchs2")
		public String editMatchs2Form(Long id, HttpServletRequest request){
			Matchs matchs;
			if (id != null) {
				matchs = currentService.findById(id);
				request.setAttribute("obj", matchs);
			}
			try {
				List<Referee> referee = refereeService.findByModel(null);
				List<Team> team = teamService.findByModel(null);
				List<Sort> sort = sortService.findByModel(null);
				if(sort!=null&&sort.size()>0){
					request.setAttribute("sort", sort);
				}
				if(referee!=null&&referee.size()>0){
					request.setAttribute("referee", referee);
				}
				if(team!=null&&team.size()>0){
					request.setAttribute("team", team);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "admin/matchs2-form";
		}
		//编辑决赛
		@RequestMapping(value = "/editMatchs1")
		public String editMatchs1Form(Long id, HttpServletRequest request){
			Matchs matchs; 
			if (id != null) {
				matchs = currentService.findById(id);
				request.setAttribute("obj", matchs);
			}
			try {
				List<Referee> referee = refereeService.findByModel(null);
				List<Team> team = teamService.findByModel(null);
				List<Sort> sort = sortService.findByModel(null);
				if(sort!=null&&sort.size()>0){
					request.setAttribute("sort", sort);
				}
				if(referee!=null&&referee.size()>0){
					request.setAttribute("referee", referee);
				}
				if(team!=null&&team.size()>0){
					request.setAttribute("team", team);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "admin/matchs1-form";
		}
	

	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all(HttpServletRequest request) {
		Matchs matchs = new Matchs();
		//referee.setCompanyId(getMe(request).getCompanyId());
		return findByModel(currentService, matchs);
	}
}